<?php
session_start();

require_once('core/core.php');

$manager = new Manager;
$customer= new Customer;

if ( $_SESSION['manager'] )
{
    
    if($_SERVER['REMOTE_ADDR']=='83.146.92.10')
    {
    }
    elseif($_SERVER['REMOTE_ADDR']=='109.191.77.233')
    {
    }
    elseif($_SERVER['REMOTE_ADDR']=='83.142.160.188')
    {
    }
    elseif($_SERVER['REMOTE_ADDR']=='176.56.22.194')
    {
    }
    elseif($_SERVER['REMOTE_ADDR']=='176.56.22.162')
    {
    }
    elseif($_SERVER['REMOTE_ADDR']=='176.226.224.111')
    {
    }
    elseif($_SERVER['REMOTE_ADDR']=='80.251.51.115')
    {
    }
    elseif($_SERVER['REMOTE_ADDR']=='77.221.215.88')
    {
    }
    elseif($_SERVER['REMOTE_ADDR']=='82.116.29.28')
    {
    }
    elseif($_SERVER['REMOTE_ADDR']=='117.219.68.59')
    {
    }
    elseif($_SERVER['REMOTE_ADDR']=='178.216.156.146')
    {
    }
    else
    {
	exit();
    }


    
    if ( is_numeric($_GET['id']) && !empty($_GET['reason']) )
    {
	if ( $manager->setBeingReason($_GET['id'],$_GET['reason'],$_REQUEST['being']) )
	{
	    $_SESSION['manager']['first_login']['being_reason'] = $_GET['reason'];
	    print json_encode(array('code'=>200));
	}
	else
	{
	    print json_encode(array('code'=>500));
	}
	exit();
    }

    if ( $_SESSION['return'] )
    {
	$goto = $_SESSION['return'];
	unset($_SESSION['return']);
	header('Location: '.$goto);
    }
    else
    {
	header('Location: admin.php');
    }
    exit();
}
else
if ( $_SESSION['customer'] )
{
    if ( $_SESSION['return'] )
    {
	$goto = $_SESSION['return'];
	unset($_SESSION['return']);
	header('Location: '.$goto);
    }
    else
    {
	header('Location: index.php');
    }
    exit();
}

if ( isset($_POST['action']) && ($_POST['action']=='login.do') )
{
    if ( $_SESSION['manager'] = $manager->auth($_POST['login'],$_POST['password']) )
    {
	header('Location: login.php');
	exit();
    }
    else
    if ( $_SESSION['customer'] = $customer->auth($_POST['login'],$_POST['password']) )
    {
	header('Location: login.php');
	exit();
    }
    else
    {
	$msg = 'Ошибка авторизации';
    }
}
?>
<html>
    <head>
	<title>Вход</title>
	<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>
	<script src="js/jquery-ui-1.8.4.custom.min.js" type="text/javascript"></script>
	<script src="js/jquery.blockUI.js" type="text/javascript"></script>
	<link href="css/redmond/jquery-ui-1.8.4.custom.css" rel="stylesheet" type="text/css"/>
    </head>
    <body>
	<script type="text/javascript">
	<!--
	$(document).ready
	(
	    function()
	    {
		$('input:text,input:password')
		    .addClass('ui-corner-all')
		    .addClass('ui-state-default')
		    .focus(function(){$(this).addClass('ui-state-active');})
		    .blur(function(){$(this).removeClass('ui-state-active');})
		    .hover
		    (
			function(){$(this).addClass('ui-state-hover');},
			function(){$(this).removeClass('ui-state-hover');}
		    );

		$('div#login').dialog
		({
		    title:	'Авторизация',
		    draggable:	false,
		    resizable:	false,
		    closeOnEscape:	false,
		    buttons:    {'Вход':function(){$(this).find('form').submit();}}
		});
		$('.ui-dialog-titlebar-close').remove();
	    }
	);
	-->
	</script>
	<div id="login">
	    <form method="post">
		<input type="hidden" name="action" value="login.do"/>
		<table width="100%">
		    <tr>
			<td align="right"><strong>Логин:</strong></td>
			<td><input type="text" name="login"/></td>
		    </tr>
		    <tr>
			<td align="right"><strong>Пароль:</strong></td>
			<td><input type="password" name="password"/></td>
		    </tr>
<?
if ( $msg )
{
?>
		    <tr class="ui-state-error">
			<td colspan="2" align="center"><?=$msg?></td>
		    </tr>
<?
}
?>
		</table>
	    </form>
	</div>
    </body>
</html>
